package com.niu1078.joe.net;

import android.content.Context;

import com.niu1078.joe.net.callback.IError;
import com.niu1078.joe.net.callback.IFailure;
import com.niu1078.joe.net.callback.IRequest;
import com.niu1078.joe.net.callback.ISuccess;
import com.niu1078.joe.ui.loader.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author :ywq .
 * time: 2017/12/19:9:21.
 * desc :
 * action:
 */

public final class RestClientBuilder {

    private String mUrl;
    private final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();


    private IRequest mIRequest;
    private IError mIError;
    private IFailure mIFailure;
    private ISuccess mISuccess;
    private File mFile;
    private RequestBody mBody;
    private Context mContext;
    private LoaderStyle mLoaderStyle;
    private String mDownloadDir = null;
    private String mExtension = null; //后缀名
    private String mName = null;

    RestClientBuilder() {
    }

    //地址
    public final RestClientBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    //参数 -
    public final RestClientBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    //参数2 -原始数据
    public final RestClientBuilder params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }

    //参数2 -原始数据
    public final RestClientBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    /**
     * 文件
     *
     * @param file 文件
     * @return
     */
    public final RestClientBuilder file(File file) {

        this.mFile = file;
        return this;
    }

    //文件上传 重载 文件的路径

    /**
     * @param file 文件路径
     * @return
     */
    public final RestClientBuilder file(String file) {

        this.mFile = new File(file);

        return this;
    }

    /**
     * 文件下载
     */

    public final RestClientBuilder name(String name) {
        this.mName = name;
        return this;
    }

    public final RestClientBuilder dir(String dir) {
        this.mDownloadDir = dir;
        return this;
    }

    public final RestClientBuilder extension(String extension) {
        this.mExtension = extension;
        return this;
    }


    public final RestClientBuilder onRequest(IRequest iRequest) {
        this.mIRequest = iRequest;
        return this;
    }

    //回调处理 成功
    public final RestClientBuilder success(ISuccess iSuccess) {
        this.mISuccess = iSuccess;
        return this;
    }

    //失败
    public final RestClientBuilder failure(IFailure iFailure) {
        this.mIFailure = iFailure;
        return this;
    }

    //错误
    public final RestClientBuilder error(IError iError) {
        this.mIError = iError;
        return this;
    }

    //进度条
    public final RestClientBuilder loader(Context context, LoaderStyle loaderStyle) {
        this.mContext = context;
        this.mLoaderStyle = loaderStyle;
        return this;
    }

    //进度条 使用默认的
    public final RestClientBuilder loader(Context context) {
        this.mContext = context;
        this.mLoaderStyle = LoaderStyle.BallClipRotatePulseIndicator;
        return this;
    }

    //构建
    public final RestClient build() {
        return new RestClient(mUrl, PARAMS,
                mDownloadDir, mExtension, mName,
                mIRequest, mIError, mIFailure,
                mISuccess, mFile, mBody, mLoaderStyle, mContext);
    }
}
